import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split, cross_val_score

# 加载数据集
breast_cancer = load_breast_cancer()
features, labels = breast_cancer.data, breast_cancer.target
# 划分训练集和测试集
features_train, features_test, labels_train, labels_test = train_test_split(features, labels, test_size=0.2,
                                                                            random_state=42)
# 定义决策树和随机森林分类器
decision_tree_classifier = DecisionTreeClassifier(random_state=0)
random_forest_classifier = RandomForestClassifier(random_state=0)
decision_tree_classifier.fit(features_train, labels_train)
random_forest_classifier.fit(features_train, labels_train)
score_decision_tree = decision_tree_classifier.score(features_test, labels_test)
score_random_forest = random_forest_classifier.score(features_test, labels_test)
print("---预测准确率---")
print("决策树: ", score_decision_tree)
print("随机森林: ", score_random_forest)

# 交叉验证评估分类器性能
decision_tree_scores = []
random_forest_scores = []
for i in range(10):
    decision_tree_score = cross_val_score(DecisionTreeClassifier(), features, labels, cv=10).mean()
    decision_tree_scores.append(decision_tree_score)
    random_forest_score = cross_val_score(RandomForestClassifier(n_estimators=25), features, labels, cv=10).mean()
    random_forest_scores.append(random_forest_score)

# 绘制评分对比曲线
plt.figure()
plt.title('Random Forest VS Decision Tree')
plt.xlabel('Index')
plt.ylabel('Accuracy')
plt.plot(range(10), decision_tree_scores, label='Decision Tree')
plt.plot(range(10), random_forest_scores, label='Random Forest')
plt.legend()
plt.show()

# 观察弱分类器数量对分类准确度的影响
random_forest_scores = []
for i in range(1, 50):
    random_forest_classifier = RandomForestClassifier(n_estimators=i)
    random_forest_score = cross_val_score(random_forest_classifier, features, labels, cv=10).mean()
    random_forest_scores.append(random_forest_score)

plt.figure()
plt.title('Random Forest')
plt.xlabel('n_estimators')
plt.ylabel('Accuracy')
plt.plot(range(1, 50), random_forest_scores, color='darkorange')
plt.show()
